//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<uniscribe.h>
///	@path	~/src/gui/text/
///	@date	2007/12/06
///	@desc	Uniscribe.

#pragma once

#include "xeres.h"

namespace xeres {

	/*!
		\class	Uniscribe
		\brief	Script analysis for text service.
	*/
	class Uniscribe
	{
	public:

		/// \ctor
		Uniscribe( HFONT font );

		/// \dtor
		~Uniscribe( void );

	//@ local

		/*!
			\brief		Modify containing string.
		*/
		WString& ModifyString( void ) { m_analyseRequired = true ; return m_buffer; }

		/*!
			\brief		Get string.
		*/
		const WString& ConstString( void ) const { return m_buffer; }

		/*!
			\brief		Get containing text size.
		*/
		size_t GetTextSize( void ) const { return m_buffer.size(); }

		/*!
			\brief		Get containing buffer.
		*/
		const wchar_t * GetBuffer( void	) const { return m_buffer.c_str(); }

		/*!
			\brief		Get containing string.
		*/
		const WString& GetString( void ) const { return m_buffer; }

		/*!
			\brief		Inserts the char at specified index.
						If index == -1, insert to the end.
		*/
		bool InsertChar( int index , wchar_t wchar );

		/*!
			\brief		Removes the char at specified index.
						If index == -1, remove the last char.
		*/
		bool RemoveChar( int index );

		/*!
			\brief		Inserts the first nCount characters of the string pStr at specified index.
						If count == -1, the entire string is inserted. If index == -1, insert to the end.
		*/
		bool InsertString( int index , const WString& str );

		/*!
			\brief		Set text as current.
		*/
		bool SetText( const WString& text );

		/*!
			\brief		Clear all text.
		*/
		void Clear( void );

		/*!
			\brief		Get character by index.
		*/
		wchar_t& operator [] ( int index );

		/*!
			\brief		Get character by index.
		*/
		wchar_t operator [] ( int index ) const;

		/*!
			\brief
		*/
		bool CPtoX( int nCP , BOOL bTrail , int * pX );

		/*!
			\brief
		*/
		bool XtoCP( int nX , int * pCP , int * pnTrail );

		/*!
			\brief
		*/
		void GetPriorItemPos( int nCP , int * pPrior );

		/*!
			\brief
		*/
		void GetNextItemPos( int nCP , int * pPrior );

		/*!
			\brief		Set tag to analyze text.
		*/
		void DesireAnalyze( void ) { m_analyseRequired = true; }

		/*!
			\brief		Analysis text.
		*/
		bool Analyze( void );

		/*!
			\brief		Set text recognized as password.
		*/
		void SetPassword( void ) { m_isPassword = true; }

		/*!
			\brief		Set fonts of uniscribe.
		*/
		void SetFont( HFONT font );

		/*!
			\brief		Clear font.
		*/
		void ClearFont( void );

	private:

		// real analyze function
		bool InternalAnalyze( void );

	//@ data

		// buffer
		WString			m_buffer;
		void *			m_analysis;

		HFONT			m_font;

		bool			m_analyseRequired;
		bool			m_isPassword;
		bool			m_spare0[2];

	};

} // namespace xeres
